package org.example.studentmanager.service.impl;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.transaction.Transactional;
import org.apache.ibatis.annotations.Param;
import org.example.studentmanager.dao.ClassDao;
import org.example.studentmanager.dao.CourseDao;
import org.example.studentmanager.entity.ClassInfo;
import org.example.studentmanager.entity.Clazz;
import org.example.studentmanager.entity.Course;
import org.example.studentmanager.entity.Student;
import org.example.studentmanager.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.ColorUIResource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private ClassDao classDao;

    // 根据教师id查询学生
    @Override
    public List<ClassInfo> findStudentsOfClass(Integer id) {
        List<ClassInfo> classInfoList = new ArrayList<ClassInfo>();
        List<Clazz> clazzList = classDao.findAClazzByTeacherId(id);
        for (Clazz clazz : clazzList) {
            classInfoList.add(new ClassInfo(clazz, classDao.findStudentsByClassId(clazz.getId())));
        }
        return classInfoList;
    }

    @Override
    public List<ClassInfo> findStudentsOfClassByIds(Integer id, Integer courseId) {
        List<ClassInfo> classInfoList = new ArrayList<ClassInfo>();
        List<Clazz> clazzList = classDao.findAClazzByIds(id, courseId);
        for (Clazz clazz : clazzList) {
            classInfoList.add(new ClassInfo(clazz, classDao.findStudentsByClassId(clazz.getId())));
        }
        return classInfoList;
    }

    @Override
    public List<Student> findStudentsByClassId(Integer id, Integer courseId) {
        Course course = courseDao.queryCourseByCourseId(courseId);
        List<Clazz> clazzList = classDao.findAClazzById(id, course.getCname());
        return classDao.findStudentsByClassId(clazzList.get(0).getId());
    }

    // 根据课程名查询班级
    @Override
    public List<Clazz> findClazzByCourseName(String name) {
        return classDao.findClazzByCourseName(name);
    }

    // 删除班级
    @Override
    public void delete(Integer id) {
        classDao.delete(id);
    }

    // 将学生退出相应的班级
    @Override
    @Transactional
    public Integer deleteStudentByClassId(Integer studentId, Integer classId) {
        return classDao.deleteStudentByClassId(studentId, classId);
    }

    // 向班级添加学生
    @Override
    public void addStudent(Integer classId, Integer studentId) {
        classDao.addStudent(classId, studentId);
    }

    // 创建班级信息
    @Override
    public void insertClass(Clazz clazz) {
        classDao.insertClass(clazz);
    }


}
